package org.jeecg.modules.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.blog.domain.DTO.BlogCommentDTO;
import org.jeecg.modules.blog.domain.entity.BlogComment;
import org.jeecg.modules.blog.domain.entity.BlogReply;
import org.jeecg.modules.blog.service.IBlogCommentService;
import org.jeecg.modules.blog.service.IBlogReplyService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description:
 * @Author: Alex
 * @Date 2022-05-01-16:45
 * @Version: V1.0
 **/
@RestController
@RequestMapping("/blog/comment")
public class BlogCommentController {

    @Autowired
    private IBlogCommentService blogCommentService;

    @Autowired
    private IBlogReplyService blogReplyService;

    @RequestMapping("/list")
    public Object list(BlogComment blogComment ,
                       @RequestParam(value = "pageNo" , defaultValue = "1")Integer pageNo,
                       @RequestParam(value = "pageSize" , defaultValue = "1")Integer pageSize,
                       HttpServletRequest request
                       ){
        System.out.println("评论过来了：：");
        QueryWrapper<BlogComment> queryWrapper =
                QueryGenerator.initQueryWrapper(blogComment , request.getParameterMap());
        queryWrapper.lambda().orderBy( true,false, BlogComment::getCreateTime);

        Page<BlogComment> page = new Page<>(pageNo , pageSize);
        IPage pageList = blogCommentService.page(page  , queryWrapper);

        List<BlogComment> records = pageList.getRecords();

        List<BlogCommentDTO> transform = Lists.transform(records, entity -> {
            BlogCommentDTO blogCommentDTO = new BlogCommentDTO();
            blogCommentDTO.setBlogComment(entity);
            LambdaQueryWrapper<BlogReply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(BlogReply::getCommentId, entity.getId());
            List<BlogReply> list = blogReplyService.list(lambdaQueryWrapper);
            blogCommentDTO.setBlogReplyList(list);
            return blogCommentDTO;
        });

        System.out.println(transform);

        pageList.setRecords(transform);

        return Result.OK(pageList);
    }

    @RequestMapping(value = "add" , method = RequestMethod.POST)
    public Object add(@RequestBody BlogComment blogComment){
        System.out.println(blogComment);
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        blogComment.setAvatar(user.getAvatar());
        blogComment.setUserId(user.getId());
        blogComment.setUsername(user.getUsername());

        blogCommentService.save(blogComment);

        return Result.OK("添加成功");


    }

}
